package com.hmdp.limiter.aop;

import com.hmdp.limiter.annotation.RateLimiter;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Collections;

@Aspect
@Component
public class RateLimiterAspect {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //限流Lua脚本
    private static final DefaultRedisScript<Long> SLIDING_WINDOW_SCRIPT;

    static {
        SLIDING_WINDOW_SCRIPT = new DefaultRedisScript<>();
        SLIDING_WINDOW_SCRIPT.setLocation(new ClassPathResource("limiter.lua"));
        SLIDING_WINDOW_SCRIPT.setResultType(Long.class);
    }

    //前置拦截 注解了rateLimiter的方法
    @Before("@annotation(rateLimiter)")
    public void doBefore(JoinPoint point, RateLimiter rateLimiter){
        System.out.println("进入切面逻辑");

        //获取注解参数
        String key = rateLimiter.key();
        long window = rateLimiter.window();
        long limit = rateLimiter.limit();

        //构建完整的限流key
        String fullKey = buildRateLimitKey(point, rateLimiter, key);
        //执行Lua脚本限流
        Long result = executeSlidingWindowScript(fullKey, window, limit);

        //判断限流结果 返回0表示被限流
        if (result != null && result == 0) {
            throw new RuntimeException(rateLimiter.message());
        }
    }
    /**
     * 执行Lua脚本限流
     * @param key 完整的限流key
     * @param window 时间窗口
     * @param limit 限流数量
     * @return 0表示被限流
     */
    private Long executeSlidingWindowScript(String key, Long window, Long limit) {
        long now = System.currentTimeMillis();
        System.out.printf("key:%s,window:%d,limit:%d\n", key, window, limit);
        return stringRedisTemplate.execute(
                SLIDING_WINDOW_SCRIPT,
                Collections.singletonList(key),
                window.toString(), limit.toString(), Long.toString(now)
        );
    }

    /**
     * 构建完整的限流key
     * @param point
     * @param rateLimiter
     * @param BaseKey
     * @return
     */
    private String buildRateLimitKey(JoinPoint point,RateLimiter rateLimiter, String BaseKey) {
        StringBuilder keyBuilder = new StringBuilder(BaseKey);

        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        //添加类名和方法
        keyBuilder.append(method.getDeclaringClass().getName())
                .append(":")
                .append(method.getName());
        //根据限流类型添加额外维度
        switch (rateLimiter.type()) {
            case IP:
                keyBuilder.append(":ip:").append(getClientIp());
                break;
            case USER:
               keyBuilder.append(":user:").append(getCurrentUserId());
                break;
            case METHOD:
            default:
                //方法级限流使用默认key
                break;
        }
        return keyBuilder.toString();
    }


    /**
     * 获取客户端ip
     */
    private String getClientIp(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && !ip.isEmpty() && !"unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取当前用户id
     */
    private String getCurrentUserId() {
        return "anonymous";
    }
}
